﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF.BaseClasses;
using PlanningGIS.Framework;
using System.Runtime.InteropServices;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Statistics
{

    /// <summary>
    /// 名    称：ReportWizard
    /// 作    用：自定义统计向导菜单
    /// 开发人员：张利勇
    /// 开发日期：2009-05-13
    /// 修改人员：
    /// 修改日期：
    /// 修改说明：
    /// </summary>
    public class ReportWizard : BaseCommand
    {
        IApplication m_Application = null;
        public ReportWizard()
        {
            base.m_caption = "自定义统计";
            base.m_toolTip = base.m_caption;
        }

        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        public override void OnClick()
        {
            if ((m_Application as IFramework).IsMapCtrlactive)
            {
                bool hasFeatureLayer = false;
                if ((m_Application.Document as IMxDocument).ActiveView.FocusMap.LayerCount == 0)
                {
                    hasFeatureLayer = false;
                    return;
                }

                IMap pMap = (m_Application.Document as IMxDocument).ActiveView.FocusMap;
                IEnumLayer layers = pMap.get_Layers(null, true);
                ILayer layer = layers.Next();
                while (layer != null)
                {
                    if (layer.Valid && (layer is IFeatureLayer))
                    {
                        hasFeatureLayer = true;
                        break;
                    }
                    layer = layers.Next();
                }
                Marshal.ReleaseComObject(layers);

                if (hasFeatureLayer == false)
                {
                    MsgHelper.ShowInfomationMsgbox("当前地图尚未加载任何矢量图层，请先加载矢量图层再进行统计！");
                    return;
                }

                PlanningGIS.Statistics.FrmCustomReport dlgReport = new PlanningGIS.Statistics.FrmCustomReport(m_Application,(m_Application as IFramework).MapControl);
                dlgReport.Show((m_Application as IFramework).MdiForm);
            }
        }

        public override bool Enabled
        {
            get
            {
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    IEnumLayer layers = (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer is IFeatureLayer)
                        {
                            Marshal.ReleaseComObject(layers);
                            return true;
                        }
                        layer = layers.Next();
                    }
                    Marshal.ReleaseComObject(layers);
                }
                return false;
            }
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.Statistics.Res." + this.GetType().Name + ".png";
                return new Bitmap(GetType().Assembly.GetManifestResourceStream(bitmapResourceName)).GetHbitmap().ToInt32();
            }
        }
    }
}
